package com.zhou.bjgl.bjgl.business_web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhou.bjgl.bjgl.business_web.entity.BTeacher;
import com.zhou.bjgl.bjgl.business_web.entity.BUserRole;
import com.zhou.bjgl.bjgl.business_web.mapper.BUserRoleMapper;
import com.zhou.bjgl.bjgl.business_web.service.CommonService;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.business_web.mapper.BTeacherMapper;
import com.zhou.bjgl.bjgl.req.b_teacher.BTeacherQueryReq;
import com.zhou.bjgl.bjgl.resp.teacher.BTeacherResp;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.business_web.service.BTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.bjgl.bjgl.util.CopyUtil;
import com.zhou.bjgl.bjgl.util.coder.SHA256Encrypt;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

import static com.zhou.bjgl.bjgl.constants.WxConstant.MY_DEFAULT_PROFILE;


/**
 * <p>
 * 教师servcie实现类
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@Service
public class BTeacherServiceImpl extends ServiceImpl<BTeacherMapper, BTeacher> implements BTeacherService {

    @Resource
    private BTeacherMapper bTeacherMapper;

    @Resource
    private BUserRoleMapper bUserRoleMapper;

    /**
     * 添加老师
     * @param req
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(BTeacherQueryReq req) {
        BTeacher bTeacher = CopyUtil.copy(req, BTeacher.class);
        //对密码进行加密
        if (!ObjectUtils.isEmpty(bTeacher.getPassword())) {
            bTeacher.setPassword(SHA256Encrypt.getSHA256StrJava(bTeacher.getPassword()));
        }else {
            bTeacher.setPassword(null);
        }
        //判断是添加还是修改
        if (ObjectUtils.nullSafeEquals("add", req.getTypeForEdit())) {
            //设置默认头像
            bTeacher.setProfileUrl(MY_DEFAULT_PROFILE);

            bTeacher.setDelStatus(0);
            int teacher = bTeacherMapper.insert(bTeacher);
            //进行用户权限设置
            BUserRole bUserRole = new BUserRole();
            bUserRole.setRoleId(3);
            bUserRole.setUserId(bTeacher.getId());
            bUserRoleMapper.insert(bUserRole);
            if (teacher == 0) {
                throw new BwException(BwExceptionEnum.INSERT_FAIL);
            }
        }else {
            bTeacherMapper.updateById(bTeacher);
        }
    }

    /**
     * 查询老师列表
     * @param req
     * @return
     */
    @Override
    public PageResp<BTeacherResp> queryTea(BTeacherQueryReq req){
        //进行分页
        PageHelper.startPage(req.getPage(), req.getSize());
        //查询记录
        List<BTeacherResp> teacherResps = bTeacherMapper.queryTea(req);

        PageResp<BTeacherResp> pageResp = new PageResp<>();
        pageResp.setPageList(teacherResps);

        //获取总页数
        PageInfo<BTeacherResp> respPageInfo = new PageInfo<>(teacherResps);
        long total = respPageInfo.getTotal();
        pageResp.setTotal(total);

        return pageResp;
    }

    /**
     * 删除老师
     * @param req
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void deleteTea(BTeacherQueryReq req) {

        try {
            bTeacherMapper.deleteById(req.getId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BwException(BwExceptionEnum.DELETE_FAILID);
        }

    }
}
